﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.Storage.Streams;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media.Imaging;

namespace EmprestaAE.Common
{
    class ByteToBitmapImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            byte[] data = value as byte[];
            BitmapImage bitmap = new BitmapImage();
            Image image = new Image();
            if (data != null && data.Length > 0)
            {                
                using (var stream = new InMemoryRandomAccessStream())
                {
                    DataWriter writer = new DataWriter(stream);
                    writer.WriteBytes(data);
                    writer.StoreAsync();                   
                    stream.Seek(0);
                    bitmap.SetSource(stream);
                    image.Source = bitmap;
                }
            }
            return image.Source;
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            throw new NotImplementedException();
        }
    }
}
